package org.jeecg.modules.util;

import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.api.xxApi.vo.XxApiFpkjMxParam;
import org.jeecg.modules.common.util.StringUtilsExt;
import org.jeecg.modules.xxgx.common.util.InvoiceCalcUtil;
import org.jeecg.modules.xxgx.djgl.constant.NumberConstant;
import org.jeecg.modules.xxgx.djgl.constant.StringConstant;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 发票工具类
 */
public class FPUtil {
	/**
	 * 对发票明细的数量、单价、不含税单据、不含税金额、税额重新计算
	 * 说明:不要删除或修改入参map的key名称
	 * @param mxItem  spsl\hsdj\bhsdj\hsje\bhsje\se
	 * @param tspz
	 */
	public static void calcItem(XxApiFpkjMxParam mxItem, String tspz){
		BigDecimal tax= InvoiceCalcUtil.toBigDecimal(StringUtilsExt.getString(mxItem.getTax()),BigDecimal.ZERO);
		String spslStr = StringUtilsExt.getString(mxItem.getSpsl());
		BigDecimal spsl=null;
		if(StringUtils.isNotEmpty(spslStr)){
			spsl=new BigDecimal(spslStr);
		}
		BigDecimal hsdj = mxItem.getHsdj();
		BigDecimal bhsdj =mxItem.getBhsdj();
		BigDecimal hsje = mxItem.getHsje();
		BigDecimal bhsje = mxItem.getBhsje();
		BigDecimal se =mxItem.getTaxje();

		//含税金额为空时，试图通过 数量 单价计算金额
		if(hsje==null||BigDecimal.ZERO.compareTo(hsje)==0){
			if(StringUtils.isNotEmpty(spslStr) && hsdj!=null){
				hsje=spsl.multiply(hsdj).setScale(NumberConstant.NUM_2,RoundingMode.HALF_UP);
			}
		}

		if(bhsje==null||BigDecimal.ZERO.compareTo(bhsje)==0){
			if(StringUtils.isNotEmpty(spslStr) && bhsdj!=null){
				bhsje=spsl.multiply(bhsdj).setScale(NumberConstant.NUM_2,RoundingMode.HALF_UP);
			}
		}

		//如果含税金额 不为空使用含税金额 进行反计算
		if(hsje!=null){
			//数量或单价有为空时重新计算
			if((spsl!=null&&BigDecimal.ZERO.compareTo(spsl)!=0) ||  (hsdj!=null&&BigDecimal.ZERO.compareTo(hsdj)!=0)){
				if(spsl==null ||BigDecimal.ZERO.compareTo(spsl)==0){
					spsl=InvoiceCalcUtil.calcQuantity(hsje,hsdj);
				} else if(hsdj==null||BigDecimal.ZERO.compareTo(hsdj)==0){
					hsdj=InvoiceCalcUtil.calcPrice(hsje,spsl);
				}
			}

			//计算不含税金额
			if(bhsje==null||BigDecimal.ZERO.compareTo(bhsje)==0){
				if(BigDecimal.ZERO.compareTo(tax)==0){
					bhsje=hsje;
				}
				else {
					if(se!=null&&BigDecimal.ZERO.compareTo(se)!=0){
						bhsje = hsje.subtract(se);
					} else {
						bhsje=InvoiceCalcUtil.calcNoTaxAmount(hsje,tax);
					}
				}
			}

			//税额
			if(se==null){
				se = hsje.subtract(bhsje);
			}

			if((bhsdj==null||BigDecimal.ZERO.compareTo(bhsdj)==0) && (spsl!=null&&BigDecimal.ZERO.compareTo(spsl)!=0)){
				if(BigDecimal.ZERO.compareTo(tax)==0){
					bhsdj=hsdj;
				}
				else{
					bhsdj=InvoiceCalcUtil.calcPrice(bhsje,spsl);
				}
			}
		} else if(bhsje!=null){
			if((spsl!=null&&BigDecimal.ZERO.compareTo(spsl)!=0) ||  (bhsdj!=null&&BigDecimal.ZERO.compareTo(bhsdj)!=0)){
				if(spsl==null ||BigDecimal.ZERO.compareTo(spsl)==0){
					spsl=InvoiceCalcUtil.calcQuantity(bhsje,bhsdj);
				} else if(bhsdj==null||BigDecimal.ZERO.compareTo(bhsdj)==0){
					bhsdj=InvoiceCalcUtil.calcPrice(bhsje,spsl);
				}
			}

			//税额
			if(se==null){
				if(BigDecimal.ZERO.compareTo(tax)==0){
					se=BigDecimal.ZERO;
				}
				else {
					se=InvoiceCalcUtil.getValue(bhsje.multiply(tax),2);
				}
			}

			//计算含税金额=不含税金额+税额
			hsje=bhsje.add(se);

			if((hsdj==null||BigDecimal.ZERO.compareTo(hsdj)==0) && (spsl!=null&&BigDecimal.ZERO.compareTo(spsl)!=0)){
				if(BigDecimal.ZERO.compareTo(tax)==0){
					hsdj=bhsdj;
				}
				else {
					hsdj = InvoiceCalcUtil.calcPrice(hsje,spsl);
				}
			}
		}
		//导入单据不为机动车类型时 对商品数量进行处理
		if (!StringConstant.STR_12.equals(tspz)) {
			if(spsl!=null){
				mxItem.setSpsl(spsl);
			}
		}
		mxItem.setHsdj(hsdj);
		mxItem.setBhsdj(bhsdj);
		mxItem.setHsje(hsje);
		mxItem.setBhsje(bhsje);
		mxItem.setTaxje(se);
	}

}
